﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.BLL.Product;
using ECommerce.Model.Product;
using Jzy.BLL.SiteAdmin;
using Jzy.Model.SiteAdmin;
using ECommerce.BLL.Advertisement;
using ECommerce.Model.Advertisement;
using Jzy.Core.Dict;
using ECommerce.Common;

namespace ECommerce.Web
{
    public partial class Default : SitePage
    {
        private ECProductBO Product
        {
            get
            {
                return ECProductBO.Instance<ECProductBO>();
            }
        }

        protected string InitID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();
                BindType();
                DispAdvertisement();
            }
        }

        private void BindType()
        {
            DictBO dict = new DictBO();
            IEnumerable<DictInfo> list = dict.GetList((int)DictItem.品牌分类);
            lv1.DataSource = list;
            lv1.DataBind();

            list = dict.GetList((int)DictItem.材料分类);
            lv2.DataSource = list;
            lv2.DataBind();

            list = dict.GetList((int)DictItem.价格分类);
            lv3.DataSource = list;
            lv3.DataBind();

            list = dict.GetList((int)DictItem.用途分类);
            lv4.DataSource = list;
            lv4.DataBind();
        }

        private void DispAdvertisement()
        {
            AdvertisementBO ad = new AdvertisementBO();
            List<AdvertisementInfo> list = ad.GetList();
            IEnumerable<AdvertisementInfo> tmp = list.Where(T => T.Name == "home1");
            if (tmp.Count() > 0)
            {
                lnkHome1.HRef = tmp.First().Href;
                imgHome1.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home2");
            if (tmp.Count() > 0)
            {
                lnkHome2.HRef = tmp.First().Href;
                imgHome2.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home3");
            if (tmp.Count() > 0)
            {
                lnkHome3.HRef = tmp.First().Href;
                imgHome3.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home4");
            if (tmp.Count() > 0)
            {
                lnkHome4.HRef = tmp.First().Href;
                imgHome4.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home5");
            if (tmp.Count() > 0)
            {
                lnkHome5.HRef = tmp.First().Href;
                imgHome5.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home6");
            if (tmp.Count() > 0)
            {
                lnkHome6.HRef = tmp.First().Href;
                imgHome6.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home7");
            if (tmp.Count() > 0)
            {
                lnkHome7.HRef = tmp.First().Href;
                imgHome7.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home8");
            if (tmp.Count() > 0)
            {
                lnkHome8.HRef = tmp.First().Href;
                imgHome8.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home9");
            if (tmp.Count() > 0)
            {
                lnkHome9.HRef = tmp.First().Href;
                imgHome9.Src = tmp.First().BigImageAbsPath;
            }

            tmp = list.Where(T => T.Name == "home10");
            if (tmp.Count() > 0)
            {
                lnkHome10.HRef = tmp.First().Href;
                imgHome10.Src = tmp.First().BigImageAbsPath;
            }
        }

        private void BindView()
        {
            List<ECProductInfo> list = this.Product.GetNewList(8);
            rptNewProduct.DataSource = list;
            rptNewProduct.DataBind();

            list = this.Product.GetRecommendList(8);
            rptRecommend.DataSource = list;
            rptRecommend.DataBind();

            list = this.Product.GetTopList(10);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Index = i + 1;
            }
            rptSaleRank.DataSource = list;
            rptSaleRank.DataBind();

            if (list.Count > 0)
            {
                this.InitID = list.First().ID.ToString();
            }

            ArticleBO article = new ArticleBO();
            IEnumerable<ArticleInfo> artilceList =  article.GetDisplayListByCode(1, 3);
            rptNews.DataSource = artilceList;
            rptNews.DataBind();


            ECProductCommentBO comment = new ECProductCommentBO();
            List<ECProductCommentInfo> commentList = comment.GetNewCommentList();
            rptComment.DataSource = commentList;
            rptComment.DataBind();
                
        }

       
    }
}